﻿using System.Windows;
using VIA.Foundation;
using VIA.Integration.Domain;
using VIA.Integration.Presentation;
using VIA.Presentation;
using VIA.Presentation.Contracts;
using VIA.Project.Domain.Command;
using VIA.SiMoJi.Domain.Option;

namespace VIA.SiMoJi.Presentation.ViewModel
{
    [ViewModel(typeof(IStationOptionView))]
    internal class StationOptionViewModel : EditorViewModel<IStationOptionView, StationOption>
    {
        #region Filed
        private readonly ICommandExecutor _commandExecutor;
        private readonly IOptionRepository optionRepository;
        private bool isLoadProjectFromFile;
        private int selectedProjectID;
        private InspectorOption _inspectorOption;
        #endregion

        #region Command

        public IRelayCommand CancelCommand { get; }

        #endregion

        #region Property

        public bool IsLoadProjectFromFile { get => isLoadProjectFromFile; set { SetProperty(ref isLoadProjectFromFile, value); } }

        public int SelectedProjectID { get { return selectedProjectID; } set { SetProperty(ref selectedProjectID, value); } }

        public InspectorOption InspectorOption { get => _inspectorOption; set => SetProperty(ref _inspectorOption, value); }

        #endregion

        public StationOptionViewModel(IServiceProvider service, ICommandExecutor commandExecutor, ICommandFactory commandFactory, IOptionRepository optionRepository) : base(service)
        {
            _commandExecutor = commandExecutor;
            this.optionRepository = optionRepository;
            CancelCommand = commandFactory.Create(Cancel);
            InspectorOption = optionRepository.Get<InspectorOption>();
        }

        public void Trigger(string address, string cmd)
        {
            _commandExecutor.Execute(address, cmd);
        }

        private void Cancel()
        {
            if (MessageBox.Show("退出不保存！", "提示！", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                View.Close();
        }


        protected override async Task Save()
        {
            try
            {
                _currentRecipe.Set(EditObject.Id, Option);
                optionRepository.Set(InspectorOption);
                await _currentRecipe.SaveAsync().ConfigureAwait(continueOnCapturedContext: false);
                await optionRepository.SaveAsync();
                MessageBox.Show("保存成功！", "提示！", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
